package com.codefish.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.codefish.entity.CapitalAccount;
import com.codefish.mapper.CapitalAccountDao;
import com.codefish.service.CapitalAccountService;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Collection;
import java.util.List;


@Service("capitalAccountService")
public class CapitalAccountServiceImpl extends ServiceImpl<CapitalAccountDao, CapitalAccount> implements CapitalAccountService {

    /**
     * 删除单个
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteOne(Integer id) {

        CapitalAccount capitalAccount = new CapitalAccount();
        capitalAccount.setId(id);
        capitalAccount.setIsDel(1);
        int deleteFlag = baseMapper.updateById(capitalAccount);
        if (1 == deleteFlag) {
            return true;
        }

        return false;
    }

    /**
     * 查询所有
     *
     * @param familyId
     * @return
     */
    @Override
    public List<CapitalAccount> getAll(Integer familyId) {

        QueryWrapper<CapitalAccount> qw = new QueryWrapper<>();
        qw.ne("is_del", 1);
        qw.eq("family_id", familyId);
        qw.orderByDesc("id");

        List<CapitalAccount> retList = baseMapper.selectList(qw);

        return retList;
    }

    /**
     * 新增单个
     *
     * @param capitalAccount
     * @return
     */
    @Override
    public int saveOne(CapitalAccount capitalAccount) {
        boolean saveFlag = this.save(capitalAccount);

        if (!saveFlag) {
            return -1;
        }

        return capitalAccount.getId();
    }

    /**
     * 确认是否可以新增或者是修改
     *
     * @param capitalAccount
     * @return
     */
    @Override
    public boolean checkFlag(CapitalAccount capitalAccount) {

        QueryWrapper<CapitalAccount> qw = new QueryWrapper<>();

        qw.eq("state", 1).ne("is_del", 1).eq("family_id", capitalAccount.getFamilyId());

        qw.eq("account_name",capitalAccount.getAccountName());

        if (ObjectUtils.isNotEmpty(capitalAccount.getId())) {
            qw.ne("id", capitalAccount.getId());
        }

        List<CapitalAccount> count = baseMapper.selectList(qw);

        if (CollectionUtils.isEmpty(count)){
            return true;
        }

        return false;
    }

    /**
     * 根据id批量查询
     * @param capitalAccountIds
     * @return
     */
    @Override
    public List<CapitalAccount> selectListByIds(List<Integer> capitalAccountIds) {
        QueryWrapper<CapitalAccount> qw = new QueryWrapper<>();
        qw.in("id",capitalAccountIds);

        return baseMapper.selectList(qw);
    }
}